Skip to main content

Overview

Duet uses Cloudflare’s AI service with the Llama 3 8B Instruct model to provide intelligent pair programming assistance. The LLM generates responses based on conversation history and can output commands for sandbox execution.

Model Configuration

The worker uses Cloudflare AI binding configured in wrangler.toml:
Source: ~/workspace/source/cf-worker/wrangler.toml:12-13 The model is accessed via the @cf/meta/llama-3-8b-instruct identifier:
Source: ~/workspace/source/cf-worker/index.ts:122-127

Message Format

The AI expects messages in a specific format:
Source: ~/workspace/source/cf-worker/index.ts:84-87

System Prompt

Duet’s AI behavior is defined by a system prompt that instructs it to be concise and use special tags for command execution:
Source: ~/workspace/source/cf-worker/index.ts:154-162 This prompt establishes:
  • Identity: Duet is a pair-programming assistant
  • Tone: Concise and action-oriented
  • Command syntax: Use <run>command</run> tags
  • Output handling: Don’t predict output, let sandbox provide it

Conversation Context

The AI receives context from recent conversation history:
Source: ~/workspace/source/cf-worker/index.ts:154-168 The agent:
  1. Includes the system prompt
  2. Adds the last 10 messages from conversation history
  3. Appends the current user message
  4. Converts message roles (“agent” → “assistant”, “user” → “user”)

Command Execution Flow

The AI can trigger sandbox commands using special tags:

1. AI Response with Commands

The AI wraps commands in <run> tags:

2. Command Extraction

The agent extracts commands using regex:
Source: ~/workspace/source/cf-worker/index.ts:185-193

3. Sandbox Execution

Each extracted command is executed in the room’s sandbox:
Source: ~/workspace/source/cf-worker/index.ts:195-205

4. Output Appending

Command outputs are appended to the AI’s response:
  • First 500 characters of stdout or stderr
  • Error messages if execution fails
  • “[no output]” if command produces nothing

5. Tag Removal

The <run> tags are stripped from the final response:
Source: ~/workspace/source/cf-worker/index.ts:207

Complete Message Flow

Source: ~/workspace/source/cf-worker/index.ts:129-183

API Request/Response

Request Format

Validated by:
Source: ~/workspace/source/cf-worker/index.ts:9-12

Response Format

Source: ~/workspace/source/internal/ai/client.go:42-47

Next Steps